﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace QLLopHoc.KetNoiDatabase
{
    public class DataAccess
    {
        protected static string _connectionString =
            "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = QLLopHoc.mdb";
        static OleDbConnection connection;
        public static void OpenConnection()
        {
            try
            {
                if (connection == null)
                    connection = new OleDbConnection(_connectionString);
                connection.Open();
            }
            catch (Exception)
            { 
            }
        }
        public static void CloseConnection()
        {
            if (connection != null)
                connection.Close();
        }
        public static DataTable execquery(string sql)
        {
            OpenConnection();
            DataTable dt = new DataTable();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            CloseConnection();
            return dt;
        }

        public static int ExecNonQuery(string sql)
        {
            OpenConnection();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            return command.ExecuteNonQuery();
        }

        //public static DataTable execquery(string sql)
        //{
        //    //throw new NotImplementedException();
        //    DataTable dt = new DataTable();
        //    return dt;
        //}
    }
}
